GBML manual / Tweens

tween_to

Makes a variable glide from where it is now to `to` over `duration` frames, speeding up or braking the way the curve says, and leaves it at `to` when it arrives. You write the variable’s name, and the engine moves it a little every frame - there is nothing to do in a Step event.

Works differently on Game Boy - The Game Boy glides up to 8 variables at once (the Advance 16), in 32 steps of the curve, over a distance of less than 1,024; a ninth jumps to its end value as the Advance's does when its places are full.

Works differently on Game Boy Color - The Game Boy Color glides up to 8 variables at once (the Advance 16), in 32 steps of the curve, over a distance of less than 1,024; a ninth jumps to its end value as the Advance's does when its places are full.

On other consoles this differs: see Side by side and How the consoles differ.

Syntax

tween_to(target, to, duration, ease) -> bool

Arguments

NameType
targetfield
toreal
durationint
easeeasing

Returns

bool

Description

Makes a variable glide from where it is now to to, over duration frames, the way the curve says. You write the variable's name; the engine moves it a little every frame and leaves it at to when it arrives. There is nothing to do in a Step event.

It works on a number variable of an instance: x, y, angle, xscale, or one you made yourself, such as hp. Name another instance's variable the way you would read it (other.y, boss.hp). A variable that holds a whole number gets the nearest whole number on every step.

Example

tween_to(x, x + 64, 60, EASE_QUAD_OUT);

This instance slides 64 pixels to the right over one second. It starts fast and brakes as it arrives.

Good to know

Cost

Starting one is a few sums. Every running glide costs a few sums a frame, about as much as a line like x = x + 1. A game that never starts one has none of it in the ROM.

The game also carries only the curves you ask for by name. When every tween_to and tween_from_to in the game says its curve outright (EASE_QUAD_OUT, not a variable that holds one), the game is built with just those curves: one curve is about 2.6 KB smaller than all sixteen, and the first seven together about 2 KB smaller. When some call gets its curve from a variable, from a script's parameter or from a script's answer, the engine cannot know which curves will come up, so it carries all sixteen, exactly as before. Either way a tween runs the curve you asked for.

On the GBA

The variable is named, not given: tween_to(x, 200, 60, EASE_QUAD_OUT) moves THIS instance’s x, tween_to(other.y, 40, 30, EASE_SINE_IN_OUT) another one’s, tween_to(boss.hp, 0, 120, EASE_LINEAR) the hp of the instance a variable called boss points at. Built-in variables (x, y, hsp, vsp, angle, xscale, yscale, depth, speed...) and the ones you made yourself both work, as long as they hold a real or an int; an int variable gets the nearest whole number on every step. A variable that belongs to nobody (a local variable, a global) cannot be tweened. Inside a script the built-in variables are the CALLER’s, as everywhere else in a script, so tween_to(x, 100, 30, EASE_LINEAR) in a script moves whoever called it; a variable only some kinds of object have (hp) needs the instance handed in (tween_to(who.hp, ...)), because a script does not know which kind of object called it. duration is in frames (60 is about a second) and 0 or less means "arrive now". The glide counts frames the way Step events run, so a paused game, a room change in progress and a cutscene hold every tween where it is. It runs just after Movement and Paths, before Collision events. Starting a tween on a variable that is already gliding replaces the old glide (the new one starts from where the variable is now); it takes no extra place. If something else also changes the variable (a hsp moving x, a line of code that writes it) the glide writes last every frame, so it wins. A whole-number variable is tweened as a real number, so its value must stay under about 524,000. Only 16 tweens (tween_max()) can glide at the same moment: when they are all busy, the variable jumps straight to `to` and this answers false - the game carries on and the goal is still reached, only the gliding is lost; tween_count() tells you how many are busy, and the debug overlay’s TWEENS row shows the most there ever were. IF THE INSTANCE IS DESTROYED while its tween is gliding, the tween just ends: it never writes into the dead instance, and never into a new instance that took the same place. A room change does the same to every instance that is not persistent; a persistent one keeps gliding into the next room. A game that never starts a tween has none of this in its ROM. Answers true when the glide started, false when there was no instance to move or no free place.

See also: Tweenstween_from_totween_stoptween_runningtween_counttween